// pages/teacher/banji/index.js
import {
  getBanji,
  delClass
} from "../../../api/teacher";
import {
  parseTime
} from "../../../utils/datetime.js";


const app = getApp()

Component({

  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    arr: [],
    refreshing: false,
    pageNo: 1,
    pageSize: 10,
    loadMore: true,
    bjid: "",
  },
  lifetimes: {
    attached: function () {
      this.loadData()
    }
  },


  /**
   * 组件的方法列表
   */

  methods: {
    data: {
      show: false,
    },

    loadData: function () {
      let data = {
        pageNo: this.data.pageNo,
        pageSize: this.data.pageSize
      }

      getBanji(data).then(res => {
        res.data.forEach(item => {
          item.time = parseTime(item.time, "{y}/{m}/{d}")
        })
        let arr = this.data.arr
        arr = arr.concat(res.data)
        this.setData({
          arr: arr,
          refreshing: false,
          loadMore: res.data.length == this.data.pageSize
        })
      })
    }, 

    onDelete: function (e) {
      var that = this.data.arr;
      console.log(that)
      var bjid = e.currentTarget.dataset.index;
      console.log((that[bjid]).id)
      wx.showModal({
        title: "提示",
        content: '确定要删除此班级吗',
        success(res) {
          if (res.confirm) {
            let data = {
              bjid: (that[bjid]).id
            }
            // console.log(data)
            delClass(data).then(res => {
              console.log("okkk", res)
              let {
                code,
                msg,
              } = res
              if (code === 500) {
                wx.showToast({
                  title: msg,
                  icon: 'none'
                })
              } else {
                wx.showToast({
                  title: "班级删除成功",
                })
                // this.onRefresh()
              }
            })
          } else if (res.cancel) {
            wx.showToast({
              title: "删除取消",
              icon:"none"
            })
          }
        }
      })
    },

    onClickShow() {
      this.setData({
        show: true
      });
    },

    onClickHide() {
      this.setData({
        show: false
      });
    },

    noop() {},

    //创建班级
    onAdd: function () {
      wx.navigateTo({
        url: '/pages/teacher/chuangjian/index',
        events: {
          add: () => {
            this.onRefresh()
          }
        }
      })
    },

    onDetail: function (e) {
      const index = e.currentTarget.dataset.index
      const banji = this.data.arr[index]
      wx.navigateTo({
        url: '/pages/teacher/banji/detail',
        success: res => {
          res.eventChannel.emit("send", banji)
        }
      })
    },

    //下拉刷新
    onRefresh: function () {
      this.setData({
        pageNo: 1,
        arr: []
      })
      this.loadData()
    },

    //上拉加载（分页）
    onMore: function () {
      if (this.data.loadMore === false) return
      let pageNo = this.data.pageNo
      pageNo++
      this.setData({
        pageNo
      })
      this.loadData()
    },

  },
})